<template>
    <div class="app-container">
        <el-row :gutter="20">
            <!--部门数据-->
            <el-col :span="4" :xs="24">
                <div class="head-container">
                    <el-input
                        v-model="deptName"
                        placeholder="请输入部门名称"
                        clearable
                        size="small"
                        prefix-icon="el-icon-search"
                        style="margin-bottom: 20px"
                    />
                </div>
                <div class="head-container">
                    <el-tree
                        :data="deptOptions"
                        :props="defaultProps"
                        :expand-on-click-node="false"
                        :filter-node-method="filterNode"
                        ref="tree"
                        default-expand-all
                        highlight-current
                        @node-click="handleNodeClick"
                    />
                </div>
            </el-col>

            <el-col :span="20" :xs="24">
                <el-form
                    :model="queryParams"
                    ref="queryForm"
                    size="small"
                    :inline="true"
                    v-show="showSearch"
                    label-width="68px"
                >
                    <el-form-item label="用户姓名" prop="nickName">
                        <el-input
                            v-model="queryParams.nickName"
                            placeholder="请输入签到/签退。用户昵称"
                            clearable
                            @keyup.enter.native="handleQuery"
                        />
                    </el-form-item>
                    <el-form-item label="真实日期" prop="realDate">
                        <el-date-picker
                            clearable
                            v-model="queryParams.realDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择真实日期"
                        ></el-date-picker>
                    </el-form-item>
                    <el-form-item>
                        <el-button
                            type="primary"
                            icon="el-icon-search"
                            size="mini"
                            @click="handleQuery"
                            >查询</el-button
                        >
                        <el-button
                            icon="el-icon-refresh"
                            size="mini"
                            @click="resetQuery"
                            >重置</el-button
                        >
                    </el-form-item>
                </el-form>

                <TableCard>
                    <el-row :gutter="10" class="mb8">
                        <el-col :span="1.5">
                            <el-button
                                type="warning"
                                plain
                                icon="el-icon-download"
                                size="mini"
                                @click="handleExport"
                                v-hasPermi="['oa:workAttendances:export']"
                            >
                                导出
                            </el-button>
                        </el-col>
                        <el-col :span="1.5">
                            <el-button
                                type="danger"
                                plain
                                icon="el-icon-upload"
                                size="mini"
                                @click="handleImport"
                                v-hasPermi="['oa:workAttendances:edit']"
                            >
                                导入外部考勤数据
                            </el-button>
                        </el-col>
                        <right-toolbar
                            :showSearch.sync="showSearch"
                            @queryTable="getList"
                        ></right-toolbar>
                    </el-row>

                    <el-table
                        :border="Global.tableShowBorder"
                        v-loading="loading"
                        :data="workAttendancesList"
                        @selection-change="handleSelectionChange"
                    >
                        <el-table-column
                            type="selection"
                            width="55"
                            align="center"
                        />
                        <el-table-column
                            label="用户姓名"
                            align="center"
                            prop="nickName"
                            width="180"
                        />
                        <el-table-column
                            label="真实日期"
                            align="center"
                            prop="realDate"
                            width="180"
                        >
                            <template slot-scope="scope">
                                <span
                                    v-if="!util.isEmpty(scope.row.realDate)"
                                    >{{
                                        parseTime(
                                            scope.row.realDate,
                                            "{y}-{m}-{d}"
                                        )
                                    }}</span
                                >
                                <span
                                    v-if="util.isEmpty(scope.row.realDate)"
                                    style="color: #e6a23c"
                                    >未有打卡记录</span
                                >
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="星期"
                            align="center"
                            prop="realDate"
                            width="150"
                        >
                            <template slot-scope="scope">
                                <span
                                    v-if="!util.isEmpty(scope.row.realDate)"
                                    >{{
                                        util.getWeekDay(scope.row.realDate)
                                    }}</span
                                >
                                <span
                                    v-if="util.isEmpty(scope.row.realDate)"
                                    style="color: #e6a23c"
                                    >未有打卡记录</span
                                >
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="年度周"
                            align="center"
                            prop="realDate"
                            width="180"
                        >
                            <template slot-scope="scope">
                                <span v-if="!util.isEmpty(scope.row.realDate)"
                                    >{{
                                        scope.row.realDate.split("-")[0]
                                    }}年第{{
                                        util.getYearWeek(scope.row.realDate)
                                    }}周</span
                                >
                                <span
                                    v-if="util.isEmpty(scope.row.realDate)"
                                    style="color: #e6a23c"
                                    >未有打卡记录</span
                                >
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="签到时间"
                            align="center"
                            prop="signInTime"
                            width="180"
                        >
                            <template slot-scope="scope">
                                <span
                                    v-if="!util.isEmpty(scope.row.signInTime)"
                                    >{{ scope.row.signInTime }}</span
                                >
                                <span
                                    v-if="util.isEmpty(scope.row.signInTime)"
                                    style="color: #f56c6c"
                                    >未打卡</span
                                >
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="签到状态"
                            align="center"
                            prop="signInStatus"
                            width="120"
                        >
                            <template slot-scope="scope">
                                <span
                                    v-if="scope.row.signInStatus == '0'"
                                    style="color: #67c23a"
                                    >正常</span
                                >
                                <span
                                    v-if="scope.row.signInStatus == '1'"
                                    style="color: #f56c6c"
                                    >迟到</span
                                >
                                <span
                                    v-if="util.isEmpty(scope.row.signInStatus)"
                                    style="color: #f56c6c"
                                    >未打卡</span
                                >
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="签退时间"
                            align="center"
                            prop="signOutTime"
                            width="180"
                        >
                            <template slot-scope="scope">
                                <span
                                    v-if="!util.isEmpty(scope.row.signOutTime)"
                                    >{{ scope.row.signInTime }}</span
                                >
                                <span
                                    v-if="util.isEmpty(scope.row.signOutTime)"
                                    style="color: #f56c6c"
                                    >未打卡</span
                                >
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="签退状态"
                            align="center"
                            prop="signOutStatus"
                            width="120"
                        >
                            <template slot-scope="scope">
                                <span
                                    v-if="scope.row.signOutStatus == '0'"
                                    style="color: #67c23a"
                                    >正常</span
                                >
                                <span
                                    v-if="scope.row.signOutStatus == '1'"
                                    style="color: #f56c6c"
                                    >早退</span
                                >
                                <span
                                    v-if="util.isEmpty(scope.row.signOutStatus)"
                                    style="color: #f56c6c"
                                    >未打卡</span
                                >
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="操作"
                            align="center"
                            class-name="small-padding fixed-width"
                            width="180"
                            fixed="right"
                        >
                            <template slot-scope="scope">
                                <el-button
                                    size="mini"
                                    type="text"
                                    icon="el-icon-edit"
                                    @click="handleUpdate(scope.row)"
                                    v-hasPermi="['oa:workAttendances:edit']"
                                >
                                    修改
                                </el-button>
                                <el-button
                                    size="mini"
                                    type="text"
                                    icon="el-icon-delete"
                                    @click="handleDelete(scope.row)"
                                    v-hasPermi="['oa:workAttendances:remove']"
                                >
                                    删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>

                    <pagination
                        v-show="total > 0"
                        :total="total"
                        :page.sync="queryParams.pageNum"
                        :limit.sync="queryParams.pageSize"
                        @pagination="getList"
                    />
                </TableCard>
            </el-col>
        </el-row>

        <!-- 添加或修改打卡记录对话框 -->
        <el-dialog
            :title="title"
            :visible.sync="open"
            width="500px"
            append-to-body
        >
            <el-form
                ref="form"
                :model="form"
                :rules="rules"
                label-width="120px"
            >
                <!--                <el-form-item label="签到/签退。用户id" prop="signUserId">-->
                <!--                    <el-input v-model="form.signUserId" placeholder="请输入签到/签退。用户id"/>-->
                <!--                </el-form-item>-->
                <el-form-item label="用户姓名" prop="nickName">
                    <el-input v-model="form.nickName" :disabled="true" />
                </el-form-item>
                <el-form-item label="签到时间" prop="signInTime">
                    <el-time-picker
                        clearable
                        v-model="form.signInTime"
                        :picker-options="{ format: 'HH:mm' }"
                        value-format="HH:mm"
                        placeholder="请选择签到时间"
                    >
                    </el-time-picker>
                </el-form-item>
                <el-form-item label="签到状态" prop="signInStatus">
                    <el-radio-group v-model="form.signInStatus">
                        <el-radio label="0">正常</el-radio>
                        <el-radio label="1">迟到</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="签退时间" prop="signOutTime">
                    <el-time-picker
                        clearable
                        v-model="form.signOutTime"
                        :picker-options="{ format: 'HH:mm' }"
                        value-format="HH:mm"
                        placeholder="请选择签退时间"
                    >
                    </el-time-picker>
                </el-form-item>
                <el-form-item label="签退状态" prop="signOutStatus">
                    <el-radio-group v-model="form.signOutStatus">
                        <el-radio label="0">正常</el-radio>
                        <el-radio label="1">早退</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="真实日期" prop="realDate">
                    <el-date-picker
                        clearable
                        v-model="form.realDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择真实日期"
                    >
                    </el-date-picker>
                </el-form-item>
                <!--                <el-form-item label="星期" prop="weekDays">-->
                <!--                    <el-input v-model="form.weekDays" placeholder="请输入星期"/>-->
                <!--                </el-form-item>-->
                <!--                <el-form-item label="周" prop="weekNo">-->
                <!--                    <el-input v-model="form.weekNo" placeholder="请输入周"/>-->
                <!--                </el-form-item>-->
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!-- 用户导入对话框 -->
        <el-dialog
            :title="upload.title"
            :visible.sync="upload.open"
            width="400px"
            append-to-body
        >
            <el-upload
                ref="upload"
                :limit="1"
                accept=".xlsx, .xls"
                :headers="upload.headers"
                :action="upload.url + '?updateSupport=' + upload.updateSupport"
                :disabled="upload.isUploading"
                :on-progress="handleFileUploadProgress"
                :on-success="handleFileSuccess"
                :auto-upload="false"
                drag
            >
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">
                    将文件拖到此处，或<em>点击上传</em>
                </div>
                <div class="el-upload__tip text-center" slot="tip">
                    <!--                    <div class="el-upload__tip" slot="tip">-->
                    <!--                        <el-checkbox v-model="upload.updateSupport"/>-->
                    <!--                        是否更新已经存在的用户数据-->
                    <!--                    </div>-->
                    <span>仅允许导入xls、xlsx格式文件。</span>
                    <el-link
                        type="primary"
                        :underline="false"
                        style="font-size: 12px; vertical-align: baseline"
                        @click="importTemplate"
                    >
                        下载模板
                    </el-link>
                </div>
            </el-upload>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitFileForm"
                    >确 定</el-button
                >
                <el-button @click="upload.open = false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listWorkAttendances,
    getWorkAttendances,
    delWorkAttendances,
    addWorkAttendances,
    updateWorkAttendances,
} from "@/api/hrm/workAttendances";
import { getUser } from "@/api/system/user";
import { getToken } from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { treeselect } from "@/api/system/dept";
export default {
    name: "WorkAttendances",
    components: { Treeselect },
    data() {
        return {
            // 部门名称
            deptName: undefined,
            // 部门树选项
            deptOptions: [],
            defaultProps: {
                children: "children",
                label: "label",
            },
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示查询条件
            showSearch: true,
            // 总条数
            total: 0,
            // 打卡记录表格数据
            workAttendancesList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                signUserId: null,
                signNickName: null,
                signInTime: null,
                signOutTime: null,
                realDate: null,
                weekDays: null,
                weekNo: null,

                deptId: null,
                nickName: "",
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {},

            // 用户导入参数
            upload: {
                // 是否显示弹出层（用户导入）
                open: false,
                // 弹出层标题（用户导入）
                title: "",
                // 是否禁用上传
                isUploading: false,
                // 是否更新已经存在的用户数据
                updateSupport: 0,
                // 设置上传的请求头部
                headers: { Authorization: "Bearer " + getToken() },
                // 上传的地址
                url:
                    process.env.VUE_APP_BASE_API +
                    "/hrm/workAttendances/importData",
            },
        };
    },
    watch: {
        // 根据名称筛选部门树
        deptName(val) {
            this.$refs.tree.filter(val);
        },
    },
    created() {
        debugger;
        this.getTreeselect();
        this.getList();
    },
    methods: {
        /** 导入按钮操作 */
        handleImport() {
            this.upload.title = "用户导入";
            this.upload.open = true;
        },
        /** 下载模板操作 */
        importTemplate() {
            this.download(
                "hrm/workAttendances/importTemplate",
                {},
                `考勤模板_${new Date().getTime()}.xlsx`
            );
        },
        // 文件上传中处理
        handleFileUploadProgress(event, file, fileList) {
            this.upload.isUploading = true;
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
            this.upload.open = false;
            this.upload.isUploading = false;
            this.$refs.upload.clearFiles();
            this.$alert(
                "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
                    response.msg +
                    "</div>",
                "导入结果",
                { dangerouslyUseHTMLString: true }
            );
            this.getList();
        },
        // 提交上传文件
        submitFileForm() {
            this.$refs.upload.submit();
        },

        /** 查询打卡记录列表 */
        getList() {
            this.loading = true;
            listWorkAttendances(this.queryParams).then((response) => {
                this.workAttendancesList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                signUserId: null,
                signNickName: null,
                signInTime: null,
                signOutTime: null,
                realDate: null,
                weekDays: null,
                weekNo: null,
            };
            this.resetForm("form");
        },
        /** 查询按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加打卡记录";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            if (row.id) {
                const id = row.id || this.ids;
                getWorkAttendances(id).then((response) => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改打卡记录";
                });
            } else {
                getUser(row.userId).then((res) => {
                    let data = {};
                    data.signUserId = row.userId;
                    data.nickName = data.nickName;
                    this.form = data;
                });
            }
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateWorkAttendances(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addWorkAttendances(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm('是否确认删除打卡记录编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delWorkAttendances(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                "hrm/workAttendances/export",
                {
                    ...this.queryParams,
                },
                `workAttendances_${new Date().getTime()}.xlsx`
            );
        },

        /** 查询部门下拉树结构 */
        getTreeselect() {
            treeselect().then((response) => {
                this.deptOptions = response.data;
            });
        },
        // 筛选节点
        filterNode(value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        },
        // 节点单击事件
        handleNodeClick(data) {
            this.queryParams.deptId = data.id;
            this.handleQuery();
        },
    },
};
</script>
